package com.hrg.rds.biz.comm;

import cn.hutool.json.JSONUtil;
import com.hrg.rds.biz.config.RdsConfiguration;
import com.hrg.seer.sdk.rbk.RbkResult;
import com.hrg.seer.sdk.rbk.RbkResultKind;
import com.hrg.seer.sdk.rbk.api.PushApiEnum;
import com.hrg.seer.sdk.rbk.msg.BaseRes;
import com.hrg.seer.sdk.rbk.msg.push.PushConfigReq;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class PushService {
	private static RbkResult ROBOT_PUSH_CONFIG_REQ(String ip) {
		int apiNo = PushApiEnum.ROBOT_PUSH_CONFIG_REQ.getApiNo();
		PushConfigReq req = PushConfigReq.builder().interval(RdsConfiguration.PushedIntervalMilliseconds).included_fields(RdsConfiguration.IncludedFields).build();
		return RdsConfiguration.AgvIpClientMap.get(ip).request(apiNo, JSONUtil.toJsonStr(req));
	}

	public static boolean pushConfig(String ip) {
		RbkResult rbkResult = ROBOT_PUSH_CONFIG_REQ(ip);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			BaseRes res = JSONUtil.toBean(rbkResult.getResStr(), BaseRes.class);
			if (res.getRet_code() == 0) {
				log.info(" 配置推送成功 ip = {}", ip);
				return true;
			}
			log.error("配置推送失败 ret_code = {}, err_msg = {}，ip = {}", res.getRet_code(), res.getErr_msg(), ip);
		}
		log.error("配置推送失败 kind = {}，ip = {}", rbkResult.getKind().name(), ip);
		return false;
	}
}
